import Vue from 'vue'
import Router from 'vue-router'

import Home from 'pages/home/Home'
import City from 'pages/city/City'
import HomeSearch from 'pages/home/components/search'

import Category from 'pages/category/Category'
import ShopCart from 'pages/shopcart/ShopCart'
import Detail from 'pages/detail/Detail'


/* 登陆注册相关的页面 */
import Landed from '../pages/login/Landed'
import Login from '../pages/login/Login'
import ForgetPas from '../pages/login/ForgetPas'
import SetPas from '../pages/login/SetPas'
import Welcome from '../pages/login/Welcome'

/* 我的页面相关的页面 */
import ProFile from 'pages/profile/ProFile'
import Infor from 'pages/profile/components/infor'
import WaitPay from 'pages/profile/components/waitPay'

Vue.use(Router)

const router = new Router({
  routes: [{
      path: '/', //默认为登陆页
      redirect: '/login'
    },
    {
      path: '/landed',
      name: 'Landed', //登陆或是注册页面
      component: Landed
    },
    {
      path: '/login',
      name: 'Login', //登陆或是注册页面
      component: Login
    },
    {
      path: '/home',
      name: 'Home', //首页
      component: Home
    },
    {
      path: '/search',
      name: 'HomeSearch', //首页搜索
      component: HomeSearch
    },
    {
      path: '/city',
      name: 'City', //城市
      component: City
    },
    {
      path: '/detail/:id',
      name: 'Detail', //商品详情
      component: Detail
    },
    {
      path: '/category',
      name: 'Category', //分类
      component: Category,
    },
    {
      path: '/cart',
      name: 'ShopCart', //购物车
      component: ShopCart
    },
    {
      path: '/profile',
      name: 'ProFile', //我的
      component: ProFile
    },
    {
      path: '/infor',
      name: 'Infor', //我的页面的个人信息页面
      component: Infor
    },
    {
      path: '/forgetpas',
      name: 'ForgetPas', //忘记密码页面
      component: ForgetPas
    },
    {
      path: '/setpas',
      name: 'SetPas', //设置密码页面
      component: SetPas
    },
    {
      path: '/welcome',
      name: 'Welcome', //欢迎页面 
      component: Welcome
    },
    {
      path: '/waitpay',
      name: 'WaitPay', //设置密码页面
      component: WaitPay
    },
  ]
})


//挂载一个陆游守卫
//to 将要访问的路径
//from 从那个路径过来的
//next 是一个函数，表示放行
//next（） 放行 next（‘/home’） 表示强制跳转到那个路径
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/landed' || to.path === '/forgetpas' || to.path === '/setpas' || to.path === '/detail')
    return next();
  //获取token
  const tokenStr = sessionStorage.getItem('token')
  if (!tokenStr)
    return next('/login');
  next();
})

export default router
